package yannan.voiplock4ble.net;


import android.content.Context;
import android.text.TextUtils;
import android.widget.ImageView;

import com.bumptech.glide.Glide;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import yannan.voiplock4ble.R;
import yannan.voiplock4ble.bean.CommonResponse;
import yannan.voiplock4ble.download.ProgressResponseBody;
import yannan.voiplock4ble.download.ProgressResponseListener;
import yannan.voiplock4ble.net.base.MyConverterFactory;
import yannan.voiplock4ble.net.base.NetworkService;
import yannan.voiplock4ble.view.GlideCircleTransform;

/**
 * Created by HOHD on 2016/9/26.
 */
public class NetUtils {
    public static String url = "http://192.168.1.194/json/";
    public static String wxlUrl = "http://192.168.1.77:8088";
    public static NetworkService networkService;

    //测试接口
//    public static String Base_Url_Down = "http://192.168.1.209:8080/";
//    public static String Base_Url = "http://192.168.1.209:8080/";

    //正式版接口
    public static String Base_Url = "http://lock.yannan.tech:8081/PerLock/";
    public static String Base_Url_Down = "http://lock.yannan.tech:8081/";

    public static String downLoadApkBaseUrl = Base_Url + "checkAppVersion/";

    public final static int EDITLOCKUSERINFO_DEL = 2;
    public final static int EDITLOCKUSERINFO_CHANGE_ADMIN = 0;
    public final static int EDITLOCKUSERINFO_CHANGE_COMMON = 1;
    public final static int EDITLOCKUSERINFO_BAN = 3;
    public final static int EDITLOCKUSERINFO_ALLOW = 4;

    /**
     * 访问网络
     *
     * @return
     */
    public static NetworkService getNetworkService() {

        if (null == networkService) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .connectTimeout(15, TimeUnit.SECONDS).build();
            Retrofit retrofit = new Retrofit.Builder()
                    .client(okHttpClient)
                    .baseUrl(Base_Url)
                    .addConverterFactory(new MyConverterFactory())
                    .build();
            networkService = retrofit.create(NetworkService.class);
        }
        return networkService;
    }


    public interface IOnEditLockKeyCallBack {
        void onSuc(CommonResponse resBody);

        void onFail(String msg);
    }


    /**
     * 加载图片
     *
     * @param mContext
     * @param imageView
     * @param errorImage
     * @param url
     */
    public static void loadImage(Context mContext, ImageView imageView, int errorImage, Object url) {
        if (mContext == null) return;

        if (errorImage == 0)
            errorImage = R.drawable.ic_admin_head;

        if (url instanceof String) {
            String urls = (String) url;
            if (!urls.contains("http"))
                urls = Base_Url_Down + urls;
            Glide.with(mContext)
                    .load(urls)
                    .error(errorImage)
                    .into(imageView);
            return;
        }
        Glide.with(mContext)
                .load(url)
                .error(errorImage)
                .into(imageView);
    }

    /**
     * 加载图片
     *
     * @param mContext
     * @param imageView
     * @param errorImage
     * @param url
     */
    public static void loadCircleImage(Context mContext, ImageView imageView, int errorImage, Object url) {
        if (mContext == null) return;
        if (TextUtils.isEmpty((String) url)) return;
        Glide.with(mContext)
                .load(url).transform(new GlideCircleTransform(mContext))
                .error(errorImage)
                .into(imageView);
    }


    /**
     * 访问网络
     *
     * @return
     */
    public static NetworkService getNetworkService(final ProgressResponseListener responseListener) {

        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .addInterceptor(
                        new Interceptor() {
                            @Override
                            public okhttp3.Response intercept(Chain chain) throws IOException {
                                //拦截
                                okhttp3.Response originalResponse = chain.proceed(chain.request());

                                //包装响应体并返回
                                return originalResponse.newBuilder()
                                        .body(new ProgressResponseBody(originalResponse.body(), responseListener))
                                        .build();
                            }
                        }
                ).build();
        Retrofit retrofit = new Retrofit.Builder()
                .client(okHttpClient)
                .baseUrl(Base_Url_Down)
                .build();
        NetworkService networkService = retrofit.create(NetworkService.class);
        return networkService;
    }
}
